package com.xx.xx;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class Formulas {
	
	@GsAnn(formula="z*abc+b-c/d*e")
	public String a(Map<String,Object> map,String[] fieldHandle){
		return runs(map,fieldHandle);
	}
	
	@GsAnn(formula="z*(abc+b)-c/d*e")
	public String b(Map<String,Object> map,String[] fieldHandle,String type){
		return new Formulas1().runs(map,fieldHandle,type);
	}
	
	public String runs(Map<String,Object> map,String[] fieldHandle){
		int fieldHandleLen = fieldHandle.length;
		String a = "";
		String sum = "";
		String b = "";
		List<String> list = new ArrayList<String>();
		for(int i=0;i<fieldHandleLen;i++){
			if(fieldHandle[i] == null){continue;}
			if(fieldHandle[i].equals("*")){
				String cs = "";
				int index = 0;
				if(fieldHandle[i-1] == null){
					for(int j=1;j<=fieldHandleLen;j++){
						if(fieldHandle[i-j] == null){continue;}
						cs = fieldHandle[i-j];
						index = i-j;
						break;
					}
				}else{
					cs = fieldHandle[i-1];
					index = i-1;
				}
				String cuuid = "_"+UUID.randomUUID().toString().replace("-", "");
				map.put(cuuid,(new BigDecimal(map.get(cs)+"").multiply(new BigDecimal(map.get(fieldHandle[i+1])+"")))+"");
				fieldHandle[index] = cuuid;
				fieldHandle[i] = null;
				fieldHandle[i+1] = null;
			}else if(fieldHandle[i].equals("/")){
				String cs = "";
				int index = 0;
				if(fieldHandle[i-1] == null){
					for(int j=1;j<=fieldHandleLen;j++){
						if(fieldHandle[i-j] == null){continue;}
						cs = fieldHandle[i-j];
						index = i-j;
						break;
					}
				}else{
					cs = fieldHandle[i-1];
					index = i-1;
				}
				String cuuid = "_"+UUID.randomUUID().toString().replace("-", "");
				map.put(cuuid, (new BigDecimal(map.get(cs)+"").divide(new BigDecimal(map.get(fieldHandle[i+1])+"")))+"");
				fieldHandle[index] = cuuid;
				fieldHandle[i] = null;
				fieldHandle[i+1] = null;
			}
		}
		for(int i=0;i<fieldHandleLen;i++){
			if(fieldHandle[i] == null){continue;}
			if(fieldHandle[i]!=null&&!fieldHandle[i].trim().equals("")){
				list.add(fieldHandle[i]);
			}
		}
		int listLen = list.size();
		for(int i=0;i<listLen;i++){
			if(!sum.equals("") && !b.equals("")){
				if(sum.equals("+")){
					a = (new BigDecimal(a).add(new BigDecimal(b))).toString();
				}else if(sum.equals("-")){
					a = (new BigDecimal(a).subtract(new BigDecimal(b))).toString();
				}else if(sum.equals("*")){
					a = (new BigDecimal(a).multiply(new BigDecimal(b))).toString();
				}else if(sum.equals("/")){
					a = (new BigDecimal(a).divide(new BigDecimal(b))).toString();
				}
				sum = "";
				b = "";
			}
			if(i == 0){
				a = map.get(list.get(i))+"";
			}else if(i%2 == 0){
				b = map.get(list.get(i))+"";
			}else{
				sum = list.get(i);
			}
		}
		if(!sum.equals("") && !b.equals("")){
			if(sum.equals("+")){
				a = (new BigDecimal(a).add(new BigDecimal(b))).toString();
			}else if(sum.equals("-")){
				a = (new BigDecimal(a).subtract(new BigDecimal(b))).toString();
			}else if(sum.equals("*")){
				a = (new BigDecimal(a).multiply(new BigDecimal(b))).toString();
			}else if(sum.equals("/")){
				a = (new BigDecimal(a).divide(new BigDecimal(b))).toString();
			}
			sum = "";
			b = "";
		}
		System.out.println(a);
		return a;
	}
	
}
